package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.NoticeListAdapter;
import com.sxm.yiwei.beans.NoticeBeans;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息公告
 */

public class NoticeMessageActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView messageList;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private int no =1;
    private List<NoticeBeans.DataBean> noticeData = new ArrayList<>();
    private ImageView noticeback;
    private String code;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou_suo_sou_suo);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        messageList = (PullToRefreshListView) findViewById(R.id.notice_list);
        backmain = (ImageView) findViewById(R.id.noticemsg_backmain);
        backmain.setOnClickListener(this);
        noticeback = (ImageView) findViewById(R.id.notice_back);
        noticeback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getNoticeList(no);

    }

        // 获取公告列表
    private void getNoticeList(int no) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETNOTICELIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("appId", appId);
        params.addHeader("token", token);
        params.addHeader("userId", userId);
        JSONObject obj = new JSONObject();
        obj.put("pageNo",no+"");
        obj.put("pageSize","20");
        params.addParameter("params",obj.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                JSONArray data = obj.getJSONArray("data");
                if (data.size()>0){
                    getData(data);
                }else {

                }
                messageList.onRefreshComplete();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                messageList.onRefreshComplete();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getData(final JSONArray result) {
        for (int i = 0; i < result.size(); i++) {
            NoticeBeans.DataBean beans = new NoticeBeans.DataBean();
            NoticeBeans.DataBean.NotificationDetailBean detailBean = new NoticeBeans.DataBean.NotificationDetailBean();
            JSONObject obj1 = JSON.parseObject(String.valueOf(result.get(i)));
            String id = obj1.getString("id");
            String notificationCode = obj1.getString("notificationCode");
            String notificationCreateTime = obj1.getString("notificationCreateTime");
            String notificationDetail = obj1.getString("notificationDetail");
            String notificationKind = obj1.getString("notificationKind");
            String notificationMessage = obj1.getString("notificationMessage");
            String notificationUpdateTime = obj1.getString("notificationUpdateTime");
            String userId = obj1.getString("userId");

            beans.setId(id);
            beans.setNotificationCode(notificationCode);
            beans.setNotificationCreateTime(Long.valueOf(notificationCreateTime));
            beans.setNotificationKind(notificationKind);
            beans.setNotificationMessage(notificationMessage);
            beans.setUserId(userId);
            beans.setNotificationUpdateTime(Long.valueOf(notificationUpdateTime));
            if (!TextUtils.isEmpty(notificationDetail)){
                JSONObject objBody = JSON.parseObject(notificationDetail);
                String dealResult = objBody.getString("dealResult");
                String advice = objBody.getString("advice");
                String detailCreateTime = objBody.getString("detailCreateTime");
                String id1 = objBody.getString("id");
                String notificationId = objBody.getString("notificationId");
                String notificationKind1 = objBody.getString("notificationKind");
                String shopName = objBody.getString("shopName");
                String userName = objBody.getString("userName");
                String userPhone = objBody.getString("userPhone");
                detailBean.setNotificationKind(notificationKind1);
                detailBean.setUserName(userName);
                detailBean.setUserPhone(userPhone);
                detailBean.setDealResult(dealResult);
                detailBean.setId(id1);
                detailBean.setNotificationId(notificationId);
                detailBean.setDetailCreateTime(Long.valueOf(detailCreateTime));
                detailBean.setAdvice(advice);

                detailBean.setShopName(shopName);
            }else {}
            beans.setNotificationDetail(detailBean);
            noticeData.add(beans);
        }
        final NoticeListAdapter adapter = new NoticeListAdapter(NoticeMessageActivity.this,noticeData);
        messageList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        messageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NoticeBeans.DataBean dataBean = noticeData.get(position-1);
                NoticeBeans.DataBean.NotificationDetailBean notificationDetail = dataBean.getNotificationDetail();
                code = dataBean.getNotificationCode();
                String kind = dataBean.getNotificationKind();
                long time = dataBean.getNotificationCreateTime();
                String type = dataBean.getNotificationCode();

                String result = notificationDetail.getDealResult();
                long resultTime = notificationDetail.getDetailCreateTime();
                String resultKind = notificationDetail.getNotificationKind();
                String shopname = notificationDetail.getShopName();
                String userName = notificationDetail.getUserName();
                String userPhone = notificationDetail.getUserPhone();
                String advice = notificationDetail.getAdvice();
                Intent intent = null;
                if (code.equals("2001")){
                    intent = new Intent(NoticeMessageActivity.this,NoticeItemContentTwoActivity.class);

                }else {
                    intent = new Intent(NoticeMessageActivity.this,NoticeItemContentActivity.class);

                }
                intent.putExtra("type",type);
                intent.putExtra("code", code);
                intent.putExtra("kind",kind);
                intent.putExtra("result",result);
                intent.putExtra("shopname",shopname);
                intent.putExtra("resultkind",resultKind);
                intent.putExtra("time",time+"");
                intent.putExtra("resulttime",resultTime+"");
                intent.putExtra("userName",userName);
                intent.putExtra("userPhone",userPhone);
                intent.putExtra("advice",advice);

                startActivity(intent);
            }
        });

        messageList.setMode(PullToRefreshBase.Mode.BOTH);

        messageList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                no++;
                getNoticeList(no);
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                no++;
                getNoticeList(no);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.noticemsg_backmain:
                startActivity(new Intent(NoticeMessageActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
}
